package com.yzh.wanandroid.vm

import android.app.Application
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.yzh.wanandroid.base.BaseFragmentViewModel
import com.yzh.wanandroid.model.StateData
import com.yzh.wanandroid.network.retrofit.RetrofitFactory
import com.yzh.wanandroid.network.retrofit.api.DataServer
import com.yzh.wanandroid.network.retrofit.responsebean.BaseData
import com.yzh.wanandroid.network.retrofit.responsebean.TitleData
import com.yzh.wanandroid.repository.BaseRepository
import com.yzh.wanandroid.utils.ERROR
import com.yzh.wanandroid.utils.LOADING
import com.yzh.wanandroid.utils.SUCCESS

class ProjectPagerFragmentVm(application: Application) : BaseFragmentViewModel(application) {

    val titleData = MutableLiveData<StateData<BaseData<List<TitleData>>>>()

    override fun onCreate(owner: LifecycleOwner) {
        super.onCreate(owner)
        loadWxArticleTitleList(owner)
    }

    private fun loadWxArticleTitleList(owner: LifecycleOwner) {
        BaseRepository<BaseData<List<TitleData>>>(owner) {
            loadData {
                RetrofitFactory.createApi(DataServer::class.java).getProjectList()
            }.success {
                titleData.value = StateData(loadingState = SUCCESS, data = it)
            }.loading {
                titleData.value = StateData(loadingState = LOADING)
            }.error { code, message ->
                titleData.value =
                    StateData(loadingState = ERROR, messageCode = code, message = message)
            }
        }
    }
}